import Toast from 'tdesign-miniprogram/toast/index';
import ActionSheet, {
    ActionSheetTheme
} from 'tdesign-miniprogram/action-sheet/index';
import {
    getActivityDetail,
    noPassActivity,
    passActivity,
    completeActivity,
    deleteActivity
} from '../../../../utils/request/manage/activityManage/activityCheckDetail';

// pages/activityDetail/activityDetail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activityInfo: {},
        classList: [],
        takeInStudents: [],
        authClass: [],
        classEmpty: {
            show: false,
            image: 'http://ldjy.asc.jx.cn/assets/draw_fly.png'
        },
        personEmpty: {
            show: false,
            image: 'http://ldjy.asc.jx.cn/assets/draw_fly.png'
        },
        showOperate: true
    },
    // 通过活动ID拉取活动信息
    getActivityById() {
        const activityId = wx.getStorageSync('activityDetailId')
        getActivityDetail(activityId).then(res => {
            this.setData({
                activityInfo: res.data.activity,
                classList: res.data.classDTOS,
                takeInStudents: res.data.takeInStudents,
                authClass: res.data.authClass
            })
            if (this.data.authClass === undefined) {
                this.setData({
                    classEmpty: {
                        show: true,
                        image: this.data.classEmpty.image
                    }
                })
            }
            if (this.data.takeInStudents === undefined || this.data.takeInStudents[0] === undefined) {
                this.setData({
                    personEmpty: {
                        show: true,
                        image: this.data.personEmpty.image
                    }
                })
            }
        })
    },

    handleFabClick() {

    },
    handleAction() {
        ActionSheet.show({
            theme: ActionSheetTheme.List,
            selector: '#t-action-sheet',
            context: this,
            align: 'left',
            description: '操作',
            items: [{
                    label: '通过',
                    icon: 'check-circle',
                    suffixIcon: 'chevron-right',
                }, {
                    label: '不通过',
                    icon: 'close-circle',
                    suffixIcon: 'chevron-right',
                }, {
                    label: '直接完成',
                    icon: 'doge',
                    suffixIcon: 'chevron-right',
                },
            ],
        });
    },
 
    handleSelected(e) {
        switch (e.detail.index) {
            case 0: // 报名活动
                this.passActivity()
                break;
            case 1: // 取消报名
                this.unPassActivity()
                break;
            case 2: // 直接完成
                this.completeActivity()
                break;
            case 3:
                this.deleteActivity();
                break;
        }

    },

    // 通过活动
    passActivity(){
        passActivity(this.data.activityInfo.activityId).then(resp => {
            if(resp.data.code === 200){
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '活动通过',
                    icon: 'check-circle',
                    direction: 'column',
                  });
            }else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: resp.data.message,
                    icon: 'uncomfortable',
                    direction: 'column',
                  });
            }
        })
    },
    // 不通过活动
    unPassActivity(){
        const examineDTO = {
            id: this.data.activityInfo.activityId,
            remark: 'info'
        }



        noPassActivity(examineDTO).then(resp => {
            if(resp.data.code === 200){
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '活动不通过',
                    icon: 'check-circle',
                    direction: 'column',
                  });
            }else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: resp.data.message,
                    icon: 'uncomfortable',
                    direction: 'column',
                  });
            }
        })
    },
    // 直接完成活动
    completeActivity(){
        completeActivity(this.data.activityInfo).then(resp => {
            if(resp.data.code === 200){
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '活动完成',
                    icon: 'check-circle',
                    direction: 'column',
                  });
            }else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: resp.data.message,
                    icon: 'uncomfortable',
                    direction: 'column',
                  });
            }
        })
    },
    deleteActivity(){
        if(this.data.activityInfo.stateId === 3){
            Toast({
                context: this,
                selector: '#t-toast',
                message: '活动已完成，无法删除',
                icon: 'uncomfortable',
                direction: 'column',
              });
            return 
        }


        deleteActivity(this.data.activityInfo).then(resp => {
            if(resp.data.code === 200){
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: '删除成功',
                    icon: 'check-circle',
                    direction: 'column',
                  });
            }else {
                Toast({
                    context: this,
                    selector: '#t-toast',
                    message: resp.data.message,
                    icon: 'uncomfortable',
                    direction: 'column',
                  });
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getActivityById()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})